<?php
namespace App\Services\Spider;

use Illuminate\Support\Arr;
// use GuzzleHttp\Client;
// use JonnyW\PhantomJs\Client as JClient;

/*
https://blog.csdn.net/Time888/article/details/78183183

*/
class TouTiaoDrive extends God
{

    // public function claws3(string $keyword)
 //   {

        // $response  = exec('/srv/phantomjs-2.1.1-linux-x86_64/bin/phantomjs --version',$output);
//         $response  = exec('pwd',$output);
        //  $response  = shell_exec('export OPENSSL_CONF=/etc/ssl/;phantomjs --version 2>&1');
// echo "<pre>";
//         var_dump($output);
        
       
//         die;

       // $client = JClient::getInstance();
        // $path= dirname(dirname(dirname(dirname(__FILE__))));
        // $path= $path.'/bin/phantomjs';
        // $client->setPhantomJs($path);
      //  $client->setPhantomJs('/bin/phantomjs');
        // $client->addOption('--ignore-ssl-errors=true');
        // $client->setTimeout(10000); 
        
        // $client->addHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.2 Safari/534.34');

        /**
         * @see JonnyW\PhantomJs\Message\Request
         **/
      //  $request = $client->getMessageFactory()->createRequest('GET','http://www.baidu.com');

        // /**
        //  * @see JonnyW\PhantomJs\Message\Response
        //  **/
       // $response = $client->getMessageFactory()->createResponse();

        // // Send the request
        // $client->send($request, $response,'./capture000000.png');
    //     $client->send($request, $response);
    //     $status = $response->getStatus() ;
    //     dd($response);
    //     if($status === 200) {

    //         // Dump the requested page content
    //         echo __LINE__.">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
    //         echo $response->getContent();
    //     }
    //     die;
    // }

    public function claws(string $keyword)
    {

        $keywordCode = urlencode($keyword);
        $out = [];
        for ($page = 1; $page <=3; $page++) {

            $content = $this->get_page($keywordCode);
            echo ($content);
            die;

            if (!$content || empty($content['data'] )) continue;

            $lists = [];

            foreach ($content['data'] as $item) {
                $isTouTiao = isset($item['title'])
                    && isset($item['group_id'])
                    && isset($item['group_source'])
                    && isset($item['has_video'])
                    && ($item['has_video'] != 1)
                    && ($item['group_source'] == 2);

                if ($isTouTiao) { // 因为有其他网站内容，只提取出头条的
                    $lists[] = [
                        'title' => $item['title'],
                        'group_id' => $item['group_id'],
                        'url' => "https://m.toutiao.com/i{$item['group_id']}/info/?i={$item['group_id']}",
                        // 'url' => "https://www.toutiao.com/a{$item['group_id']}", // pc 需要 cookie
                    ];


                }
            }
            if (!$lists) return null;
           dd($lists);
            foreach ($lists as $post) {
                $title = $content = '';
                $ua = rand_ua();

                $postUrl = Arr::get($post, 'url');
                try {
                    $json = $this->getResponses($postUrl, ["user-agent: $ua",], false); // phone json
                } catch (\Throwable $e) {
                    $this->logError($e);
                    continue;
                }

                $pack = json_decode($json, true);

                if (isset($pack['data']['title']) && isset($pack['data']['content'])) {
                    $title = $pack['data']['title'];
                    $content = $pack['data']['content'];
                    $content = preg_replace('/\s+/', ' ', $content);
                    //写一个简介,去掉content内的标签，只取汉字的200字
                    $short_article = strip_tags($content);
                    //写一个将多个空格，合并成一个空格的正则表达式
                    $short_article = preg_replace('/\s+/', ' ', $short_article);

                    $short_article = mb_substr($short_article, 0, 200);

                    // if (mb_strlen($content)> mb_strlen($short_article)) {
                    //     $short_article .=" ...";
                    // }

                }

                if (empty($title) || empty($content)) {
                    continue;
                }

                $title = strip_tags($title);
                $content = $this->stripTags($content);
                $out[] = [
                    'url' => $postUrl,
                    'title' => $title,
                    'short_article'=>$short_article,
                    'content' => $content,
                    'keyword' => $keyword,
                ];

            }

        }

        return $out;
    }

    protected function get_page($keyword) {

        $headers = [
            'Content-Type' => 'application/x-www-form-urlencoded',
            'Host' => 'www.toutiao.com',
            'Referer' => 'https://www.toutiao.com',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763',
            'X-Requested-With' => 'XMLHttpRequest',
            'cookie'=>'msToken=Jn1_nNM9w7X0ZlGHJ7nlbzh8MWmfSaFLYs64Io3ce9wupnojVMEav1IqMh3vQY9P4r0xg98_5Zva3PpKR9afiL9kQRjEs6GvOym0pQX8HibWz2RCVJGEtOx2ZZboG3k=; ttwid=1%7Cv0jJe9RUbKO6eeVEv_2uJOwuje8fhRwJHTypb9SO-wM%7C1695310884%7Cf0c3fb02e7f6c9a619f7da83e1ea03432438914bfc642af1fcc6b36ce8ad1141'
        ];

        // $base_url = 'https://www.toutiao.com/api/search/content?&from=question&cur_tab_title=question&keyword='.$keyword;

        $base_url = "https://so.toutiao.com/search?dvpf=pc&source=input&keyword=%E6%88%91%E4%BB%AC%E6%89%93%E5%B7%A5%E4%BA%BA&pd=question&action_type=search_subtab_switch&page_num=0&from=question&cur_tab_title=question";

        // $base_url = "https://www.toutiao.com/api/search/content/?aid=24&app_name=web_search&offset=-10&format=json&keyword=%E6%89%93%E5%B7%A5%E4%BA%BA&autoload=true&count=10&en_qc=1&cur_tab=1&from=question&pd=synthesis&timestamp=1695311638&cur_tab_title=question";


        $response = $this->getResponses($base_url,$headers);
        return $response;

        // if (!$response) return null;
        // $json = json_decode($response, true);
        // return $json;
    }

    protected function getResponses($url, $headers = [], $isPost = true)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POST, $isPost);

        // curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        // curl_setopt($curl, CURLOPT_PROXY, IP::get());

        if ($response = curl_exec($curl)) {
            curl_close($curl);
            return $response;
        }

        if ($message = curl_error($curl)) {
            // throw new \Exception(" >>>>>>>>>>>> TouTiao error：$url => {$message}");
            log('TouTiao error：' . $url . ' => ' . $message);
            return null;
        }
        curl_close($curl);
        return $response;
    }
}
